package cn.wolfcode;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


public class HttpClientDemo {
    public static void main(String[] args) throws IOException {

        String urlTest = "https://www.shicimingju.com/chaxun/list/22484.html";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet =new HttpGet(urlTest);
        httpGet.setHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
                "(KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 Edg/109.0.1518.52");
        HttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        Reader reader =new InputStreamReader(content);
        BufferedReader bufferedReader =new BufferedReader(reader);
        StringBuilder sb =new StringBuilder(1080);
        String str ="";
        while ((str=bufferedReader.readLine())!=null) {
            sb.append(str).append("\n");
        }

        Document doc = Jsoup.parse(sb.toString());
        Elements select = doc.select("div#item_div");
        Elements title = select.select("h1");
        Elements author = select.select("div.niandai_zuozhe");
        Elements info = select.select("div.item_content");
        String[] infos = info.text().split("。");
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~");

        System.out.println(title.text());
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@");
        System.out.println(author.text());
        List<String> list = Arrays.stream(infos).map(s -> s.trim() + "。").collect(Collectors.toList());
        for (String s : list) {
            System.out.println("==========================");
            System.out.println(s);
        }
    }
}
